//
//  RootViewController.m
//  costa-postcards
//
//  Created by 黄 鹏霄 on 12-12-13.
//  Copyright (c) 2012年 test. All rights reserved.
//

#import "RootViewController.h"
#import "AFHTTPClient.h"
#import "Image.h"
#import "Canvas.h"
#import "BottomBar.h"
#import "ColorSelectController.h"
#import "ComponentSelectController.h"
#import "MovedImageView.h"


#define kDuration 0.7   // 动画持续时间(秒)

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize canvas = _canvas;
@synthesize bar = _bar;

- (void)loadView
{
    [super loadView];
    
//    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
//    [self.view addSubview:image];
//    
//    UIButton *upload = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    upload.frame = CGRectMake(0, image.frame.size.height + 10, 100, 50);
//    [upload setTitle:@"upload test" forState:UIControlStateNormal];
//    [upload addTarget:self  action:@selector(uploadTest:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:upload];

    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self setCanvas:[[Canvas alloc] initWithFrame:CGRectMake(20, 80, 280, 200)]];
    [self canvas].delegate = self;
    [self.view addSubview:[self canvas]];
    
    [self setBar:[[BottomBar alloc]initWithFrame:CGRectMake(0, 372, 320, 44)]];
    
    [self bar].backgroundColor = [UIColor redColor];
    [self bar].delegate = self;
    [self.view addSubview:[self bar]];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"upload test" forState:UIControlStateNormal];
    button.frame = CGRectMake(5, 20, 80, 50);
    [button addTarget:self action:@selector(uploadTest:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Versenden" style:UIBarButtonItemStyleBordered target:self action:@selector(versenden:)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - uploadTest
- (void)uploadTest:(UIButton *)sender
{
    [Image uploadImage:nil];
}

#pragma mark - canvas delegate
-(void)willCanvasFlip:(UIButton *)sender
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:kDuration];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.canvas cache:YES];
    
    NSUInteger green = [[self.canvas subviews] indexOfObject:self.canvas];
    NSUInteger blue = [[self.canvas subviews] indexOfObject:self.canvas];
    [self.canvas exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
    
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用某个方法
    //[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    [UIView commitAnimations];

}

#pragma mark - bottom bar delegate
- (void)willColorSelectControllerDisplay:(UIButton *)sender
{
    ColorSelectController *controller = [[ColorSelectController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:controller];
    [self.navigationController presentModalViewController:navi animated:YES];
    [controller release];
    [navi release];
}

- (void)willBorderSelectControllerDisplay:(UIButton *)sender
{
    
}

- (void)willSelectComponentDisplay:(UIButton *)sender
{
    ComponentSelectController *controller = [[ComponentSelectController alloc]init];
    controller.delegate = self;
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:controller];
    [self.navigationController presentModalViewController:navi animated:YES];
    [controller release];
    [navi release];
}

#pragma mark - parent delegate
- (void)modalViewWillDisappear:(UIViewController *) viewController selectedImage:(UIImage *) image
{
    float imageHeight = [self canvas].frame.size.height;
    float imageWidth = imageHeight / image.size.height * image.size.width;
    MovedImageView *imageView = [[MovedImageView alloc]initWithFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
    
    [imageView setImage:image];
    [[self canvas] addSubview:imageView];
    
    [viewController dismissModalViewControllerAnimated:YES];
}



@end
